package com.example.demo.services.impl;

import com.example.demo.common.AppResult;
import com.example.demo.common.ResultCode;
import com.example.demo.dao.ArticleMapper;
import com.example.demo.dao.ArticleReplyMapper;
import com.example.demo.exception.ApplicationException;
import com.example.demo.model.Article;
import com.example.demo.model.ArticleReply;
import com.example.demo.model.User;
import com.example.demo.services.IArticleReplyService;
import com.example.demo.services.IArticleService;
import com.example.demo.services.IUserService;
import com.example.demo.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
@Slf4j

public class ArticleReplyServiceImpl implements IArticleReplyService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ArticleReplyMapper articleReplyMapper;

    @Autowired
    private IArticleService articleService;

    @Autowired
    private IUserService userService;

    @Override
    public void create(ArticleReply articleReply) {
        //判断是否为空
        if(articleReply == null || articleReply.getArticleId()==null
                || StringUtil.isEmpty(articleReply.getContent())){
            //日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //设置默认值
        articleReply.setReplyId(null);
        articleReply.setReplyUserId(null);
        articleReply.setLikeCount(0);
        articleReply.setState((byte)0);
        articleReply.setDeleteState((byte)0);
        Date date = new Date();
        articleReply.setCreateTime(date);
        articleReply.setUpdateTime(date);

        //写入数据库
        int row =articleReplyMapper.insertSelective(articleReply);
        //判断是否插入成功
        if(row !=1){
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }

        //更新文章回复数量
        articleService.addOneReplyCountId(articleReply.getArticleId());

        //日志
        log.info("回复成功 article id=" + articleReply.getArticleId());

    }

    @Override
    public List<ArticleReply> selectByArticleId(Long articleId) {
        //判断是否为空
        if(articleId==null){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //查询文章回复
        List<ArticleReply> articleReplies = articleReplyMapper.selectByArticleId(articleId);
        return articleReplies;
    }

    @Override
    public List<Article> selectByUserId(Long userId) {
        if (userId ==null){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        User user=userService.selectById(userId);

        if(user == null){
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }

        List<Article> articles=articleMapper.selectByUserId(userId);
        return articles;
    }
}

//    @Override
//    public void create(ArticleReply articleReply) {
//
//    }

//    @Override
//    public void create(ArticleReply articleReply) {
//        //判断是否为空
//        if(articleReply == null){
//            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
//            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
//        }
//        //获取文章
//        Article article=articleMapper.selectByPrimaryKey(articleReply.getArticleId());
//        //判断文章是否存在
//        if(article == null){
//            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
//            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
//        }
//
//        //判断文章状态
//        if(article.getState() != 0 || article.getDeleteState() != 0){
//            log.warn(ResultCode.FAILED_ARTICLE_BANNED.toString());
//            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
//        }
//
//        //设置默认值
//        articleReply.setState((byte)0);
//        articleReply.setDeleteState((byte)0);
//        articleReply.setLikeCount(0);
//        Date date = new Date();
//        articleReply.setCreateTime(date);
//        articleReply.setUpdateTime(date);
//
//        //插入回复语句
//        int row = articleReplyMapper.insertSelective(articleReply);
//        //判断是否插入成功
//        if(row !=1){
//            log.warn(ResultCode.FAILED_CREATE.toString());
//            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
//        }
//        //更新文章回复数量
//        Article updateArticle=new Article();
//        updateArticle.setId(article.getId());
//        updateArticle.setReplyCount(article.getReplyCount()+1);
//        row=articleMapper.updateByPrimaryKeySelective(updateArticle);
//         //判断是否更新成功
//        if(row !=1) {
//            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
//            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
//        }
//        log.warn(ResultCode.SUCCESS.toString());
//
//    }

